import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        
        toolBar=QToolBar()
        btn = QPushButton('LoadFile')
        btn.clicked.connect(self.LoadFile)
        toolBar.addWidget(btn)

        btn = QPushButton('ReLoad')
        btn.clicked.connect(self.ReLoad)
        toolBar.addWidget(btn)

        btn = QPushButton('Backward')
        btn.clicked.connect(self.Backward)
        toolBar.addWidget(btn)

        btn = QPushButton('Home')
        btn.clicked.connect(self.Home)
        toolBar.addWidget(btn)

        btn = QPushButton('Forward')
        btn.clicked.connect(self.Forward)
        toolBar.addWidget(btn)
        layout.addWidget(toolBar)        

        toolBar=QToolBar()

        cb = QCheckBox('OpenExternalLinks')
        cb.stateChanged.connect(self.OnOpenExternalLinksChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('OpenLinks')
        cb.stateChanged.connect(self.OnOpenLinksChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('Italic')
        cb.stateChanged.connect(self.OnItalicChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('Underline')
        cb.stateChanged.connect(self.OnUnderlineChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Alignment:'))
        alignment = QComboBox()
        alignment.addItems(Qt.AlignmentFlag.__members__.keys())
        alignment.currentTextChanged.connect(self.AlignmentChanged)
        toolBar.addWidget(alignment)

        toolBar.addWidget(QLabel('LineWrapMode:'))
        lineWrapMode = QComboBox()        
        lineWrapMode.addItems(QTextEdit.LineWrapMode.__members__.keys())
        lineWrapMode.currentTextChanged.connect(self.LineWrapModeChanged)
        toolBar.addWidget(lineWrapMode)

        toolBar.addWidget(QLabel('AutoFormatting:'))
        AutoFormatting = QComboBox()        
        AutoFormatting.addItems(QTextEdit.AutoFormattingFlag.__members__.keys())
        AutoFormatting.currentTextChanged.connect(self.AutoFormattingChanged)
        toolBar.addWidget(AutoFormatting)  
        layout.addWidget(toolBar)

        toolBar2=QToolBar()  

        toolBar2.addWidget(QLabel('WordWrapMode:'))
        wordWrapMode = QComboBox()        
        wordWrapMode.addItems(QTextOption.WrapMode.__members__.keys())
        wordWrapMode.currentTextChanged.connect(self.WordWrapModeChanged)
        toolBar2.addWidget(wordWrapMode)
        
        toolBar2.addWidget(QLabel('FontFamily:'))
        font = QFontComboBox()
        font.currentFontChanged.connect(self.FontChanged)
        toolBar2.addWidget(font)

        toolBar2.addWidget(QLabel('FontSize:'))
        #fontweight = QSpinBox()
        #fontweight.setRange(5,40)
        #fontweight.valueChanged.connect(self.FontSizeChanged)
        #toolBar2.addWidget(fontweight)
        fontSize=QSlider()
        fontSize.setOrientation(Qt.Orientation.Horizontal)
        fontSize.setRange(5,40)
        fontSize.valueChanged.connect(self.FontSizeChanged)
        toolBar2.addWidget(fontSize)

        btn = QPushButton('ShowHtml')
        btn.clicked.connect(self.ShowHtml)
        toolBar2.addWidget(btn)

        btn = QPushButton('ShowText')
        btn.clicked.connect(self.ShowText)
        toolBar2.addWidget(btn)

        btn = QPushButton('ShowMarkdown')
        btn.clicked.connect(self.ShowMarkdown)
        toolBar2.addWidget(btn)        
        
        layout.addWidget(toolBar2)
        
        self.textBrowser=QTextBrowser()        
        layout.addWidget(self.textBrowser)

        self.setLayout(layout)
    def LoadFile(self):
        file=QFileDialog().getOpenFileName()        
        if file[0] is None:return        
        self.textBrowser.setSource(QUrl().fromLocalFile(file[0]))
        self.parent.OnShowMessage(f'LoadFile {file[0]}')
    def ReLoad(self):
        self.textBrowser.reload()
        self.parent.OnShowMessage(f'reload')
    def Home(self):
        self.textBrowser.home()
        self.parent.OnShowMessage(f'home')
    def Backward(self):
        self.textBrowser.backward()
        self.parent.OnShowMessage(f'backward')
    def Forward(self):
        self.textBrowser.forward()
        self.parent.OnShowMessage(f'forward')
    def OnOpenExternalLinksChanged(self,state):
        self.textBrowser.setOpenExternalLinks(state)
        self.parent.OnShowMessage(f"Set OpenExternalLinks={state}")
    def OnOpenLinksChanged(self,state):
        self.textBrowser.setOpenLinks(state)
        self.parent.OnShowMessage(f"Set OpenLinks={state}")
    def OnItalicChanged(self,state):
        self.textBrowser.setFontItalic(state)
        self.parent.OnShowMessage(f"Set FontItalic={state}")
    def OnUnderlineChanged(self,state):
        self.textBrowser.setFontUnderline(state)
        self.parent.OnShowMessage(f"Set FontUnderline={state}")
    def AlignmentChanged(self,text):
        alignment = Qt.AlignmentFlag[text]
        self.textBrowser.setAlignment(alignment)
        self.parent.OnShowMessage(f'Set Alignment: {alignment}')
    def LineWrapModeChanged(self,text):
        lineWrapMode=QTextEdit.LineWrapMode[text]
        self.textBrowser.setLineWrapMode(lineWrapMode)
        self.parent.OnShowMessage(f'Set LineWrapMode: {lineWrapMode}')
    def AutoFormattingChanged(self,text):
        autoFormatting=QTextEdit.AutoFormattingFlag[text]
        self.textBrowser.setAutoFormatting(autoFormatting)
        self.parent.OnShowMessage(f'Set AutoFormatting: {autoFormatting}')
    def WordWrapModeChanged(self,text):
        wordWrapMode=QTextOption.WrapMode[text]
        self.textBrowser.setWordWrapMode(wordWrapMode)
        self.parent.OnShowMessage(f'Set WordWrapMode: {wordWrapMode}')    
    def FontChanged(self,font):
        family = font.family()
        self.textBrowser.setFontFamily(family)
        self.parent.OnShowMessage(f'Set FontFamily: {family}')
    def FontSizeChanged(self,value):
        self.textBrowser.setFontPointSize(value)
        self.parent.OnShowMessage(f'Set FontSize: {value}')
    def ShowHtml(self):
        self.parent.OnShowMessage(f'html: {self.textBrowser.toHtml()}')    
    def ShowText(self):
        self.parent.OnShowMessage(f'text: {self.textBrowser.toPlainText()}')
    def ShowMarkdown(self):
        self.parent.OnShowMessage(f'Markdown: {self.textBrowser.toMarkdown()}')
    
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet